import React, { useEffect, useState } from 'react';
import Web3 from 'web3';
import UserListABI from '../contract/ABIs/UserList.json'; // 用户列表合约 ABI
import MarketplaceABI from '../contract/ABIs/Marketplace.json'; // 用户列表合约 ABI
import UserABI from '../contract/ABIs/User.json'; // 用户合约 ABI
import MaterialListAddr from '../contract/ADDRESSes/MaterialListAddr';
import UserListAddr from '../contract/ADDRESSes/UserListAddr';


const Balance = () => {
    const { userAddr } = useParams();

    const [balance, setBalance] = useState(0);

    const web3 = new Web3(window.ethereum);
    // 实例化用户列表合约
    const UserListContract = new web3Instance.eth.Contract(
        UserListABI,
        UserListAddr
    );
    const userContract = new web3.eth.Contract(UserABI, userAddr);
    useEffect(() => {
        const fetchBalance = async () => {
            const balance = await userContract.methods.viewBalance().call();
            setBalance(balance);
        };
        fetchBalance();
    }, [userAddr]);

    return (
        <div>
            <h2>我的余额</h2>
            <p>{balance.toString()} RMB</p>
        </div>
    );
};

export default Balance;
